package com.magina.antiPro;

import com.google.common.collect.Maps;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;

public class TimeMath {


    @Test
    public void Tests() {
        assertEquals("makeReadable(0)", "00:00:00", this.makeReadable(0));
        assertEquals("makeReadable(5)", "00:00:05", this.makeReadable(5));
        assertEquals("makeReadable(60)", "00:01:00", this.makeReadable(60));
        assertEquals("makeReadable(86399)", "23:59:59", this.makeReadable(86399));
        assertEquals("makeReadable(359999)", "99:59:59", this.makeReadable(359999));
    }

    public static String makeReadable(int seconds) {
        // Do something
        if (seconds < 0 || seconds > 359999) {
            return "";
        }
        int hour = 0;
        int min = 0;
        int senc = 0;
        if (seconds / 3600 >= 1) {
            hour = seconds / 3600;
            int next = seconds % 3600;
            if (next / 60 >= 1) {
                min = next / 60;
                senc = seconds % 60;
            } else {
                senc = next;
            }
        } else {
            if (seconds / 60 >= 1) {
                min = seconds / 60;
                senc = seconds % 60;
            } else {
                senc = seconds;
            }
        }
        return String.format("%s:%s:%s", returnStr(hour), returnStr(min), returnStr(senc));

    }

    private static String returnStr(int x) {
        if (0 <= x && x < 10) {
            return "0" + x;
        }
        return x + "";
    }

    @Test
    public void testWork() {
        String wrok1 = "结算管理";
        String wrok2 = "回迁管理";
        int num1 = 0;
        int num2 = 0;
        do {
            num1 = (int) (Math.random() * 100);
            num2 = (int) (Math.random() * 100);
            System.out.println(String.format("candy----%s", num1 > num2 ? wrok1 : wrok2));
            System.out.println(String.format("magina----%s", num1 > num2 ? wrok2 : wrok1));
        } while (num1 == num2);

    }

    @Test
    public void test() {
        System.out.println("Mix Fixed Tests");
        assertEquals("2:eeeee/2:yy/=:hh/=:rr", this.mix("Are they here", "yes, they are here"));
        assertEquals("1:ooo/1:uuu/2:sss/=:nnn/1:ii/2:aa/2:dd/2:ee/=:gg",
                this.mix("looping is fun but dangerous", "less dangerous than coding"));
        assertEquals("1:aaa/1:nnn/1:gg/2:ee/2:ff/2:ii/2:oo/2:rr/2:ss/2:tt",
                this.mix(" In many languages", " there's a pair of functions"));
        assertEquals("1:ee/1:ll/1:oo", this.mix("Lords of the Fallen", "gamekult"));
        assertEquals("", this.mix("codewars", "codewars"));
        assertEquals("1:nnnnn/1:ooooo/1:tttt/1:eee/1:gg/1:ii/1:mm/=:rr",
                this.mix("A generation must confront the looming ", "codewarrs"));
    }

    public static String mix(String s1, String s2) {
        // your code
        //统计字符串m1的各个字母频率
        Map<String, Long> m1 = Stream.of(s1.replaceAll("[^a-z]", "").split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        //统计字符串m2的各个字母频率
        Map<String, Long> m2 = Stream.of(s2.replaceAll("[^a-z]", "").split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        Set<String> set1 = m1.keySet();
        Set<String> set2 = m2.keySet();
        Set<String> set3 = new HashSet<>();
        set3.addAll(set1);
        set3.addAll(set2);
        List<String> result = set3.stream().map(e -> {
            StringBuilder sb = new StringBuilder();
            if (m1.get(e) != null && m2.get(e) != null && m1.get(e) > 1 && m1.get(e) > m2.get(e)) {
                for (int i = 0; i < m1.get(e); i++) {
                    sb.append(e);
                }
                return "1:" + sb.toString();
            } else if (m1.get(e) != null && m2.get(e) != null && m1.get(e) > 1 && m1.get(e) == m2.get(e)) {
                for (int i = 0; i < m1.get(e); i++) {
                    sb.append(e);
                }
                return "=:" + sb.toString();
            } else if (m1.get(e) != null && m2.get(e) != null && m2.get(e) > 1 && m2.get(e) > m1.get(e)) {
                for (int i = 0; i < m2.get(e); i++) {
                    sb.append(e);
                }
                return "2:" + sb.toString();
            } else if (m1.get(e) != null && m2.get(e) == null && m1.get(e) > 1) {
                for (int i = 0; i < m1.get(e); i++) {
                    sb.append(e);
                }
                return "1:" + sb.toString();
            } else if (m2.get(e) != null && m1.get(e) == null && m2.get(e) > 1) {
                for (int i = 0; i < m2.get(e); i++) {
                    sb.append(e);
                }
                return "2:" + sb.toString();
            } else {
                return "";
            }
        }).filter(e -> e.length() > 0).sorted(Comparator.comparing(String::length).reversed().thenComparing(Function.identity())).collect(Collectors.toList());
        return String.join("/", result);

    }

    public static int dblLinear(int n) {
        // your code
        return 0;
    }

    private static void testing(int actual, int expected) {
        assertEquals(expected, actual);
    }

    @Test
    public void test123123123() {
        System.out.println("Fixed Tests dblLinear");
        testing(this.dblLinear(10), 22);
        testing(this.dblLinear(20), 57);
        testing(this.dblLinear(30), 91);
        testing(this.dblLinear(50), 175);
        testing(this.dblLinear(6000), 80914);
    }

    public static  void getLastNum(int n,Map map) {
        if(n==1){
            return ;
        }
        int lastInt =0;
        if ((n - 1) % 3 == 0) {
//            count++;
             lastInt = (n - 1) / 3;

        } else if ((n - 1) % 2 == 0) {
//            count++;
             lastInt = (n - 1) / 2;
        } else {
            throw new RuntimeException("illegal num!");
        }
        map.put(lastInt,n-lastInt);
          getLastNum(lastInt,map);
    }

    public static void getArray(int n,List<Integer> list,List<Integer> initList ) {
            List<Integer> nextInitList =Lists.newArrayList();
            for (int j = 0; j < initList.size(); j++) {
                int nextNum1 = initList.get(j)*2+1;
                int nextNum2 = initList.get(j)*3+1;
                list.add(nextNum1);
                list.add(nextNum2);
                nextInitList.add(nextNum1);
                nextInitList.add(nextNum2);
            }
             //list =  list.stream().sorted(Comparator.comparing(Function.identity())).distinct().collect(Collectors.toList());
        Integer listSize =  list.stream().distinct().collect(Collectors.toList()).size();
        if(listSize > n){
            for (int j = 0; j < nextInitList.size(); j++) {
                int nextNum1 = nextInitList.get(j)*2+1;
                int nextNum2 = nextInitList.get(j)*3+1;
                list.add(nextNum1);
                list.add(nextNum2);
            }
            return ;
        }
        getArray(n,list,nextInitList);


    }

}
